home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Transactor
/
Transactor_23_1988_Transactor_Publishing.d64
/
brk trap.pal
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2023-02-26
|
4KB
|
153 lines
10 sys700
1000 ;********************************
1010 ;* *
1020 ;* simulating a trap with brk *
1030 ;* -------------------------- *
1040 ;* *
1050 ;* *
1060 ;* brk vector is diverted so *
1070 ;* that "invisible" subroutines *
1080 ;* can be called. *
1090 ;* *
1100 ;* *
1110 ;* - by tom hughes v022287 - *
1120 ;* *
1130 ;********************************
1140 ;
1150 ;c64 equates
1160 ;
1170 cbinv = $0316 ;brk vector (2)
1180 chrout = $ffd2 ;output a char
1190 clrchn = $ffcc ;i/o to defaults
1200 getin = $ffe4 ;input a char
1210 memory = $8d ;temp storage (2)
1220 oldbrk = $8b ;storage for standard brk (2)
1230 stack = $0100 ;65xx stack location
1240 tidyup = $febc ;recover from interrupt
1250 *= $c000 ;sys 32768
1260 ;------------------------------
1270 ;set brk vector to our routine
1280 ;------------------------------
1290 ;in actual use, this would be a subroutine
1300 ;called once to divert the brk vector.
1310 ;
1320 jsr clrchn
1330 sei ;disable interrupts
1340 ldx cbinv
1350 ldy cbinv+1
1360 stx oldbrk ;save the old brk vector
1370 sty oldbrk+1
1380 ldx #<newbrk ;then set new vector
1390 ldy #>newbrk
1400 stx cbinv
1410 sty cbinv+1
1420 cli ;enable interrupts
1430 ;
1440 ;------------------------------
1450 ;demo brk handler
1460 ;------------------------------
1470 ;
1480 ;this is just an example of how you
1490 ;would use brk from within a program
1500 ;
1510 demo5 ldy #0
1520 demo10 lda prompt,y ;print "number?"
1530 beq demo20
1540 jsr chrout
1550 iny
1560 bne demo10
1570 demo20 jsr getin ;check the keyboard
1580 cmp #3 ;(if stop key, quit)
1590 beq quit
1600 cmp #"1" ;for numbers 1 thru 3
1610 bcc demo20
1620 cmp #"4"
1630 bcs demo20
1640 jsr chrout
1650 and #%00001111 ;make # hex 1 - 3
1660 sta trpnm ;save in our own program
1670 lda #13 ;print a carriage gosub
1680 jsr chrout
1690 brk ;execute trap
1700 trpnm .byt 0 ;(trap #)
1710 jmp demo5 ;after brk, prg continues here
1720 ;
1730 quit sei
1740 ldx oldbrk
1750 ldy oldbrk+1
1760 stx cbinv
1770 sty cbinv+1
1780 cli
1790 rts ;back to basic
1800 ;
1810 prompt .byt 13,13
1820 .asc "number (1 - 3)? "
1830 .byt 0
1840 ;==============================
1850 ;new brk routine
1860 ;==============================
1870 ;
1880 ;entry (1) interrupts disabled (except nmi)
1890 ; so jiffy clock is off.
1900 ;
1910 ;(2) on entry stack looks like this...
1920 ; (assuming old sp was at $f6)
1930 ;
1940 ; $01f6 <- old sp
1950 ; $01f5 pch (stack+6)
1960 ; $01f4 pcl (stack+5)
1970 ; $01f3 sr (stack+4)
1980 ; $01f2 .a (stack+3)
1990 ; $01f1 .x (stack+2)
2000 ; $01f0 .y (stack+1)
2010 ; $01ef <- current sp
2020 ;
2030 ;(3) expects trap # after brk
2040 ; (this location can be found by
2050 ; using the pc saved on stack -1.)
2060 ;
2070 newbrk tsx ;get current sp to .x
2080 lda stack+6,x ;to find pc-high
2090 sta memory+1
2100 lda stack+5,x ;and pc-low on the stack
2110 sta memory ;save this address
2120 bne new10 ;and subtract -1 from it
2130 dec memory+1 ;so we can locate trap #
2140 new10 dec memory
2150 ldy #0
2160 lda (memory),y ;get trap #
2170 tay ;adjust it so 1-3
2180 dey ;is now 0-2
2190 tya
2200 asl a ;multiply this # by 2
2210 tay
2220 lda table,y ;and use it to look up
2230 sta memory ;trap addresses
2240 iny
2250 lda table,y
2260 sta memory+1
2270 jmp (memory) ;(NULL) to a trap routine
2280 ;
2290 ;trap addresses
2300 ;
2310 table .word trap1
2320 .word trap2
2330 .word trap3
2340 ;
2350 ;//////////////////////////////
2360 ;demo trap routines
2370 ;//////////////////////////////
2380 ;
2390 trap1 lda #"1"
2400 jsr chrout
2410 jmp tidyup ;must end with this
2420 ;
2430 trap2 lda #"2"
2440 jsr chrout
2450 jmp tidyup
2460 ;
2470 trap3 lda #"3"
2480 jsr chrout
2490 jmp tidyup
2500 .end